/**
 * 删除重复文件
 */

var fs = require('fs'),
    crypto = require('crypto');

var d = new Date();

var dir = process.argv[2] || 'D:\\test\\';

if (!fs.existsSync(dir)) {
    console.log('目录 ' + dir + ' 不存在');
    return;
}
console.log('处理目录 ' + dir);

var sizeCache = {}; //文件大小值缓存
function SIZE(file) {
    var size = sizeCache[file];
    if (size == undefined) {
        size = fs.statSync(file).size;
        sizeCache[file] = size;
    }
    return size;
}

var md5Cache = {}; //MD5值缓存
function MD5(file) {
    var md5 = md5Cache[file];
    if (md5 == undefined) {
        var data = fs.readFileSync(file);
        md5 = crypto.createHash('md5').update(data).digest('hex');
        md5Cache[file] = md5;
    }
    return md5;
}

var files = fs.readdirSync(dir);

var temp = []; //保存完全不同的文件
for (i in files) {
    var flag = true;
    for (j in temp) {
        if (SIZE(dir + files[i]) == SIZE(dir + temp[j]) && MD5(dir + files[i]) == MD5(dir + temp[j])) {
            flag = false;
            console.log('删除:' + files[i]);
            fs.unlinkSync(dir + files[i]);
            break;
        }
    }
    if (flag) {
        temp.push(files[i]);
    }
}

console.log('耗时 ' + (new Date().getTime() - d.getTime()) + ' 毫秒');